﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CinemaWS.Model;
using CinemaWS.Model.CustomEntities;

namespace CinemaWS.Repository
{
    public static class MovieRepository
    {
        private static CinemaServiceEntities dataEntities = new CinemaServiceEntities();

        public static MovieEntity ConvertToEntity(Movie movie)
        {
            return new MovieEntity
            {
                ID = movie.ID,
                TitleEN = movie.TitleEN,
                TitleVI = movie.TitleVI,
                Genre = movie.Genre,
                Description = movie.Description,
                ImageUrl = movie.ImageUrl,
                MovieStudio = movie.MovieStudio,
                Director = movie.Director,
                Writer = movie.Writer,
                Actors = movie.Actors,
                Language = movie.Language,
                Version = movie.Version,
                Country = movie.Country,
                TrailerUrl = movie.TrailerUrl,
                Rating = movie.Rating.HasValue ? movie.Rating.Value : 0,
                Discount = movie.Discount.HasValue ? movie.Discount.Value : 0,
                Length = movie.Length.HasValue ? movie.Length.Value : 0,
                StartedDate = movie.StartedDate.HasValue ? movie.StartedDate.Value : DateTime.Now,
                EndedDate = movie.EndedDate.HasValue ? movie.EndedDate.Value : DateTime.Now,
            };
        }

        public static MovieEntity[] ConvertToArray(List<Movie> list)
        {
            MovieEntity[] movieList = new MovieEntity[list.Count()];
            for (int i = 0; i < list.Count(); i++)
            {
                Add(i, movieList, ConvertToEntity(list[i]));
            }
            return movieList;
        }

        private static void Add(int index, MovieEntity[] list, MovieEntity movie)
        {
            list[index++] = movie;
        }

        public static MovieEntity[] GetMoviesOfShowTimeInCurrentDayToNext7Days()
        {
            try
            {
                DateTime next7days = DateTime.Now.AddDays(7);
                var movieList = (from movies in dataEntities.Movie
                                 join showtimes in dataEntities.ShowTime
                                 on movies.ID equals showtimes.Movie.ID
                                 where DateTime.Compare(showtimes.Date.Value, DateTime.Now) >= 0
                                 && DateTime.Compare(showtimes.Date.Value, next7days) <= 0
                                 select movies).ToList();
                return ConvertToArray(movieList);
            }
            catch
            {
                return null;
            }
        }

        // Maybe need to use?
        public static MovieEntity[] GetAll()
        {
            try
            {
                var list = dataEntities.Movie.ToList();
                return ConvertToArray(list);
            }
            catch
            {
                return null;
            }
        }

        public static MovieEntity GetDetail(int movieID)
        {
            try
            {
                var movie = dataEntities.Movie.First(mv => mv.ID == movieID);
                return ConvertToEntity(movie);
            }
            catch
            {
                return null;
            }
        }

        public static MovieEntity[] GetMoviesByCinemaOfShowTimeInCurrentDayToNext7Days(int cinemaID)
        {
            try
            {
                DateTime next7days = DateTime.Now.AddDays(7);
                var movieList = (from movies in dataEntities.Movie
                                 join showtimes in dataEntities.ShowTime
                                 on movies.ID equals showtimes.Movie.ID
                                 where showtimes.Cinema.ID == cinemaID
                                 && DateTime.Compare(showtimes.Date.Value, DateTime.Now) >= 0
                                 && DateTime.Compare(showtimes.Date.Value, next7days) <= 0
                                 select movies).ToList();
                return ConvertToArray(movieList);
            }
            catch
            {
                return null;
            }
        }

        public static MovieEntity[] GetAllPager(int page, int itemPerPage)
        {
            try
            {
                int startPage = (page - 1) * itemPerPage;
                var list = dataEntities.Movie.Skip(startPage).Take(itemPerPage).ToList();
                return ConvertToArray(list);
            }
            catch
            {
                return null;
            }
        }
        // =======  Maybe need to use?
    }
}
